package com.squirrel.michale;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/20 7:50 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode6322 {

    public boolean checkValidGrid(int[][] grid) {
        int n = grid.length;
        int[][] matrix = new int[n * n][2];

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                int step = grid[i][j];
                matrix[step] = new int[]{i, j};
            }
        }
        int x = 0;
        int y = 0;
        for (int i = 1; i < matrix.length; i++) {
            int nxdiff = Math.abs(matrix[i][0] - x);
            int nydiff = Math.abs(matrix[i][1] - y);
            if ((nxdiff == 1 && nydiff == 2) || (nxdiff == 2 && nydiff == 1)){
                x = matrix[i][0];
                y = matrix[i][1];
                continue;
            }else {
                return false;
            }
        }
        return true;
    }
}
